package com.example.blog.module.config;

import com.alibaba.fastjson.JSON;
import com.example.blog.module.entity.VO.Result;
import com.example.blog.module.service.ArticleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.Set;

/**
 * @author B站：码神之路
topic发送的队列名称
 */
//@Slf4j
//@Component
//@RequiredArgsConstructor
//@RocketMQMessageListener(topic = "blog-update-article",consumerGroup = "blog-update-article-group")
//public class ArticleListener implements RocketMQListener<Long> {
//
//    private final ArticleService articleService;
//
//    private final StringRedisTemplate redisTemplate;
//
//    @Override
//    public void onMessage(Long id) {
//        log.info("收到的消息:{}",id);
//        //做什么了，更新缓存
//        //1. 更新查看文章详情的缓存
//        Long articleId = id;
//        String params = DigestUtils.md5Hex(articleId.toString());
//        String redisKey = "view_article::ArticleController::findArticleById::"+params;
//        Result articleResult = articleService.findArticleById(articleId);
//        redisTemplate.opsForValue().set(redisKey, JSON.toJSONString(articleResult), Duration.ofMillis(5 * 60 * 1000));
//        log.info("更新了缓存:{}",redisKey);
//        //2. 文章列表的缓存 不知道参数,解决办法 直接删除缓存
////        Set<String> keys = redisTemplate.keys("listArticle*");
////        keys.forEach(s -> {
////            redisTemplate.delete(s);
////            log.info("删除了文章列表的缓存:{}",s);
////        });
//    }
//}

